package array;

import java.util.LinkedList;
import java.util.List;

//todo 可以简化为两次遍历
public class ShortestDistanceToACharacter821 {
    public int[] shortestToChar(String s, char c) {
        int[] result = new int[s.length()];
        for (int i = 0; i < result.length; i++) {
            result[i] = Integer.MAX_VALUE;
        }
        LinkedList<Integer> indexs = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                indexs.add(i);
            }
        }
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < indexs.size(); j++) {
                result[i] = Math.min(result[i], Math.abs(i - indexs.get(j)));
            }
        }
        return result;
    }

}
